//
//  Goods.m
//  ShoutBoss
//
//  Created by Luck Mstone on 6/16/09.
//  Copyright 2009 MS. All rights reserved.
//

#import "Goods.h"
#import "cocos2d.h"
#import "Constants.h"
#import "Good.h"

@implementation Goods

@synthesize goods;

-(id) init
{
	self = [super init];
	if(self != nil){		
		//初始化工作...
		self.goods = [[NSMutableArray alloc] initWithCapacity:5];
		
		//添加good图片
		srand(time(NULL));
		for (int i = 0 ; i < GoodsNum - 1 ; i++) {
			Good *good = [Good spriteWithFile:@"good.png"];
			
			float tempx = rand()%300 + 150;
			good.position = ccp(tempx, 110);
			good.type = rand()%12;
			Good *temp;
			int j;
			for (j = 0 ; j < i ; j++ ) {
				temp = ((Good *)[goods objectAtIndex:j]);
				if(good.type == temp.type || CGRectIntersectsRect(
						CGRectMake(good.position.x, good.position.y, [good contentSize].width, [good contentSize].height),
							  CGRectMake(temp.position.x, temp.position.y, temp.contentSize.width, temp.contentSize.height)))
				{
					break;
				}
			}
			if(j < i)
			{
				i--;
				continue;
			}
			
			[goods addObject:good];
			[self addChild:good z:2 tag:i + 1];
		}
		
	}
	
	return self;
}

- (void) dealloc
{
	[goods removeAllObjects];
	[goods release];
	[super dealloc];
}

@end
